<?php 
	
	/**
	 * @author marcio.alves
	 *
	 */
	class Logger extends Base {
		
		private static $debug = null;
		private static $info = null;
		private static $error = null;
		private static $all = null;
		private static $datePatern = "d/m/Y H:i:s";
		private static $isLogEnabled = true;
		
		private static function getPath() {
			return "../" . Messages::getConfig("appconfig.app.dir.name") . "/" . Messages::getConfig("appconfig.log.dir");	
		}
		
		private static function getDebugLogger() {
			$path = self::getPath();
			$logprefix = Messages::getConfig("appconfig.app.dir.name");
			
			if( self::$debug == null ) {
				if( !opendir($path) ) {
					mkdir($path);
				}
				self::$debug = fopen($path . "/" . $logprefix . "_debug.log","a");
			} 
			return self::$debug; 			
		}
		
		private static function getInfoLogger() {
			$path = self::getPath();
			$logprefix = Messages::getConfig("appconfig.app.dir.name");
						
			if( self::$info == null ) {
				if( !opendir($path) ) {
					mkdir($path);
				}
				self::$info = fopen($path . "/" . $logprefix . "_info.log","a");
			} 
			return self::$info; 			
		}
		
		private static function getErrorLogger() {
			$path = self::getPath();
			$logprefix = Messages::getConfig("appconfig.app.dir.name");
						
			if( self::$error == null ) {
				if( !opendir($path) ) {
					mkdir($path);
				}
				self::$error = fopen($path . "/" . $logprefix . "_error.log","a");
			} 
			return self::$error; 			
		}
		
		private static function getAllLogger() {
			$path = self::getPath();
			$logprefix = Messages::getConfig("appconfig.app.dir.name");
						
			if( self::$all == null ) {
				if( !opendir($path) ) {
					mkdir($path);
				}
				self::$all = fopen($path . "/" . $logprefix . "_all.log","a");
			} 
			return self::$all; 			
		}
		
		private static function closeDebugLogger() {
			fclose(self::$debug);	
		}
		
		private static function closeInfoLogger() {
			fclose(self::$info);	
		}
		
		private static function closeErrorLogger() {
			fclose(self::$error);	
		}
		
		private static function closeAllLogger() {
			fclose(self::$all);	
		}
		
		private static function makePrefix($type, $clazz) {
			$prefix = "[$type] ";
			$format = Messages::getConfig("appconfig.log.date.format");
			if( $format == null || $format == "" ) {
				$prefix .= date(self::$datePatern);
			} else {
				$prefix .= date($format);
			}
			
			$temp = Util::get_class($clazz);
			
			if( $temp == "" ) {
				$prefix .= " [" . $clazz . "] - ";	
			} else {
				$prefix .= " [" . $temp . "] - ";
			}
			return $prefix;
		}
		
		public static function debug($clazz, $message) {
			if( self::$isLogEnabled ) {
				$temp = self::getDebugLogger();
				$prefix = self::makePrefix("DEBUG", $clazz);			
				$msg = "\n" . $prefix . $message;			
				fwrite($temp, $msg);
				self::all($msg);
			}
		}
		
		public static function info($clazz, $message) {
			if( self::$isLogEnabled ) {
				$temp = self::getInfoLogger();
				$prefix = self::makePrefix("INFO", $clazz);			
				$msg = "\n" . $prefix . $message;			
				fwrite($temp, $msg);
				self::all($msg);
			}
		}
		
		public static function error($clazz, $message) {
			if( self::$isLogEnabled ) {
				$temp = self::getErrorLogger();
				$prefix = self::makePrefix("ERROR", $clazz);
				$msg = "\n" . $prefix . $message;			
				fwrite($temp, $msg);
				self::all($msg);
			}
		}
		
		private static function all($message) {
			$temp = self::getAllLogger();
			fwrite($temp, $message);
		}
	}
?>